package jcolorlinez;

import java.awt.Color;
import java.awt.Graphics;

import javax.swing.BorderFactory;
import javax.swing.JPanel;
import javax.swing.border.BevelBorder;

@SuppressWarnings("serial")
public class ChessPanel extends JPanel {
	public final static Color[] COLORS = new Color[]{Color.RED, Color.GREEN, Color.BLUE, Color.MAGENTA, Color.ORANGE, Color.CYAN};
	private BoardFrame board = null;
	private int row = -1;
	private int col = -1;
	public ChessPanel(BoardFrame board, int row, int col) {
		this.board = board;
		this.row = row;
		this.col = col;
		this.setBorder(BorderFactory.createBevelBorder(BevelBorder.RAISED));
	}
	private int getValue() {
		return board.grid.get(row, col);
	}
	private boolean isSelected() {
		return row==board.grid.getSelectedRow() && col==board.grid.getSelectedCol();
	}
	private Color getChessColor() {
		if (getValue() > 0) {
			return COLORS[getValue() - 1];
		} else { // getValue()==0 : no chess here
			return null;
		}
	}
	@Override
	public void paintComponent(Graphics g) {
		super.paintComponent(g);
		if (getChessColor() != null) {
			g.setColor(getChessColor());
			g.fillOval(getWidth()/16, getHeight()/16, getWidth()*7/8, getHeight()*7/8);
		}
		if (isSelected()) {
			g.setColor(Color.BLACK);
			g.drawOval(getWidth()/16, getHeight()/16, getWidth()*7/8, getHeight()*7/8);
		}
	}
}
